define(["jquery","bbcare", "as/app/gms/search.ui",
    "scprit/department/vm/assessmentScreening/assessmentScreeningList.servers.js",
    "scprit/department/vm/assessmentScreening/assessmentScreeningQuestion.servers.js"
],function($,$bb,$ui,$servers,$dataDeal){
    var event ={};
    event.toSearch = function(type,val){
        var searchFn =  function(dt,flag){
            $("#infoLists").addClass("show");
            if(dt.data.rows.length > 0){
                for(var i = 0;i<dt.data.rows.length;i++){
                    dt.data.rows[i].age =  $dataDeal.age($dataDeal.dateDiff($dataDeal.getNowFormatDate(),dt.data.rows[i].birthday),dt.data.rows[i].pregnancyDay);
                }
                $ui.searchListInit({"id":"#infoLists","data":dt.data.rows});
                $("#infoLists li").data("rows",dt.data.rows);
            }else{
                if(flag){
                    $("#infoLists").html("该患者可能还没有建档");
                }
                if(!flag){
                    var key= $.trim($("#searchUser").val());
                    if(key){
                        var params2 = $dataDeal.searchParams(key,'1');
                        $dataDeal.search(params2,searchFn,'1');
                    }
                }
            }
            $("#infoLists").off("click","li");
            $("#infoLists").on("click","li",function(){
                var rows = $(this).data("rows");
                var index = $(this).attr("index");
                var userId = $(this).attr("userId");
                var week = parseInt(rows[index].pregnancyDay*1/7) + "周"+ rows[index].pregnancyDay*1%7+"天";
                var obj = {
                    "type":type,
                    "userId":userId,
                    "birthday":rows[index].birthday,
                    "pregnancDay":rows[index].pregnancyDay,
                    "userName":rows[index].patientName,
                    "gender":rows[index].gender,
                    "week":week,
                    "phoneNum":rows[index].phoneNum
                };
                if(type == "GMS"){
                    $bb.openPage("gmsDetail",{"info":obj});
                }
            });
        };
        if(val){
            var params = $dataDeal.searchParams(val);
            $dataDeal.search(params,searchFn);
        }

        $('#myModal').on('hidden.bs.modal', function (e) {
            $("#searchUser").val("");
            $("#infoLists").removeClass("show").html("");
        });

        $("#searchUser").on("input propertychange",function(){
            var key= $.trim($(this).val());
            if(key){
                var params = $dataDeal.searchParams(key);
                $dataDeal.search(params,searchFn);
            }else{
                $("#infoLists").removeClass("show");
            }
        });
        $("#toSearch").click(function(){
            var key= $.trim($("#searchUser").val());
            if(key){
                var params = $dataDeal.searchParams(key);
                $dataDeal.search(params,searchFn);
            }else{
                $("#infoLists").removeClass("show");
            }
        });
    };
    return event;
});
